import { IotDevice } from "_common/entity/iot/IotDevice";
import { RequestMethod, createRequest } from "_common/request";
import { AxiosInstance } from "axios";

export class ApiDevice {
  /**
   * @description 请求实例
   * @type {RequestMethod}
   * @memberof ApiDevice
   * */
  request: RequestMethod;
  url: string = "api/iot/device";

  /**
   * @description 构造函数
   * @param config axios 配置
   */
  public constructor(instance: AxiosInstance) {
    // axios 实例化
    this.request = createRequest(instance);
  }

  /** 创建设备 */
  createDevice(data: any) {
    return this.request.post<boolean>(this.url + "", data);
  }

  /** 更新设备 */
  updateDevice(data: any) {
    return this.request.put<boolean>(this.url + "", data);
  }

  /** 删除设备 */
  deleteDevice(id: number) {
    return this.request.delete<boolean>(this.url + "/" + id);
  }

  /** 获取设备信息 */
  getDeviceInfo(id: number) {
    return this.request.get<IotDevice>(this.url + "/" + id);
  }

  /** 获取设备列表 */
  getDeviceList() {
    return this.request.get<IotDevice[]>(this.url + "/get-list");
  }

  // 获取设备分页列表
  getPageList(params: any) {
    return this.request.get<any>(this.url + "/get-page", params);
  }

  // 获取设备树
  getDeviceTree() {
    return this.request.get<any[]>(this.url + "/get-tree", {});
  }

  // 导入文件
  import(terminalId: number, dtCode: number, formData: FormData) {
    return this.request.post<boolean>(
      this.url + "/upload/" + terminalId + "/" + dtCode,
      formData
    );
  }

  // 手动加脂,返回加脂记录的id
  addGrease(deviceCode?: number) {
    return this.request.post<number>(this.url + "/add-fat/" + deviceCode, {});
  }

  // 获取加脂操作记录表
  getGreaseRecord(deviceCode?: number) {
    return this.request.get<any[]>(
      this.url + "/get-grease-record/" + deviceCode,
      {}
    );
  }

  // 获取加脂结果
  getGreaseResult(id?: number) {
    return this.request.get<any>(this.url + "/get-grease-result/" + id, {});
  }
}
